package com.neouton.crm.pm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.pm.entity.BackMoneyPlanVo;
import com.neouton.crm.pm.entity.BackMoneyRecordVo;
import com.neouton.crm.pm.entity.ContractInfoVO;
import com.neouton.crm.pm.service.IContractInfoService;
import com.neouton.crm.sales.entity.SalesChangeVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 合同相关controller
 * @Author libingkun
 * @Date 2022年11月30日
 * @Version 1.0
 *          初始化迁移
 */
@RestController
@RequestMapping("contractInfo")
public class ContractInfoController {

    @Resource
    private IContractInfoService contractInfoService;

    /**
     * 根据合同id查询得到回款计划内容
     * @param id
     * @return
     */
    @GetMapping("/searchBackMoneyPlanBycontractId/{id}")
    public ResponseAPI searchBackMoneyPlanBycontractId(@PathVariable Integer id){
        return ResponseAPI.successData(contractInfoService.searchBackMoneyPlanBycontractId(id));
    }

    /**
     * 根据合同id查询得到回款计划内容
     * @param id
     * @return
     */
    @PostMapping("/searchBackMoneyPlanByBmpId/{id}")
    public ResponseAPI searchBackMoneyPlanByBmpId(@PathVariable Integer id){
        return ResponseAPI.successData(contractInfoService.searchBackMoneyPlanByBmpId(id));
    }
    /**
     * 保存合同信息
     * @param contractInfoVO
     * @return
     */
    @PostMapping("/saveOrUpdateContractInfo")
    public ResponseAPI saveOrUpdateContractInfo(@RequestHeader("token") String token,@RequestBody ContractInfoVO contractInfoVO){
        contractInfoVO.setCreator(AuthUtils.getUserId());
        contractInfoVO.setLastUpdateBy(AuthUtils.getUserId());
        Integer contractId = contractInfoService.saveOrUpdateContractInfo(contractInfoVO,token);
        return ResponseAPI.successData(contractId);
    }


    /**
     * 修改项目
     *
     * @param contractInfoVO
     * @return
     */
    @PostMapping("/updateContractInfoAfterPass/")
    public ResponseAPI updateContractInfoAfterPass(@RequestBody ContractInfoVO contractInfoVO, @RequestHeader("token") String token) {
        contractInfoService.updateContractInfoAfterPass(contractInfoVO,token);
        return  ResponseAPI.successData("success");
    }


    @PostMapping("/queryContractInfoFromCache/{contractId}")
    public ResponseAPI queryContractInfoFromCache(@PathVariable(value = "contractId") int contractId, @RequestHeader("token") String token) {
        ContractInfoVO contractInfoVO = contractInfoService.queryContractInfoFromCache(contractId, token);
        return ResponseAPI.successData(contractInfoVO);
    }

    /**
     * 替换回款计划内容在修改合同信息后
     *
     * @param contractInfoVOs
     * @return
     */
    @PostMapping("/replaceBackMoneyByContractId/")
    public ResponseAPI replaceBackMoneyByContractId(@RequestBody List<ContractInfoVO> contractInfoVOs, @RequestHeader("token") String token) {
        List<ContractInfoVO> replaceBackPlans = contractInfoService.replaceBackMoneyByContractId(contractInfoVOs,token);
        return  ResponseAPI.successData(replaceBackPlans);
    }


    /**
     * 通过审批ID获取合同信息
     * @param approvalId
     * @return
     */
    @PostMapping("/getContractInfoByApprovalId/{approvalId}")
    public ResponseAPI getContractInfoByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        ContractInfoVO contractInfoVO = contractInfoService.queryContractInfoByapprovalId(approvalId);
        return ResponseAPI.successData(contractInfoVO);
    }

    /**
     * 通过审批ID获取历史合同信息
     * @param approvalId
     * @return
     */
    @PostMapping("/getContractInfoHistoryByApprovalId/{approvalId}")
    public ResponseAPI getContractInfoHistoryByApprovalId(@PathVariable(value = "approvalId") Integer approvalId){
        ContractInfoVO contractInfoVO = contractInfoService.getContractInfoHistoryByApprovalId(approvalId);
        return ResponseAPI.successData(contractInfoVO);
    }


    /**
     * 保存合同信息
     * @param contractId
     * @return
     */
    @PostMapping("/getContractInfoById/{contractId}")
    public ResponseAPI getContractInfoById(@PathVariable(value = "contractId") int contractId){
        ContractInfoVO contractInfoVO = contractInfoService.queryContractInfoById(contractId);
        return ResponseAPI.successData(contractInfoVO);
    }

    /**
     * 获取当日最大合同单号
     * @return
     */
    @PostMapping("/getContractMaxNumByDate")
    public ResponseAPI getContractNumByDate(){
        String numStr = contractInfoService.getContractMaxNumByDate();
        return ResponseAPI.successData(numStr);
    }


    /**
     * 查询变更信息
     *
     * @param contractInfoId
     * @return
     */
    @PostMapping("queryChangeInfo/{contractInfoId}")
    public ResponseAPI queryChangeInfo(@PathVariable(value = "contractInfoId") Integer contractInfoId) {
        List<SalesChangeVO> result = contractInfoService.queryChangeInfo(contractInfoId);
        return ResponseAPI.successData(result);
    }

    /**
     * 合同信息驳回审批
     * @param contractInfoVO 合同参数
     * @return
     */
    @PostMapping("rejectionContractInfo")
    public ResponseAPI rejectionContractInfo(@RequestBody ContractInfoVO contractInfoVO,@RequestHeader("token") String token) {
        Boolean flage = contractInfoService.rejectionContractInfo(contractInfoVO,token);
        return ResponseAPI.successData(flage);
    }

    /**
     * 合同信息通过审批
     * @param contractInfoVO 合同参数
     * @return
     */
    @PostMapping("approveContractInfo")
    public ResponseAPI approveContractInfo(@RequestBody ContractInfoVO contractInfoVO,@RequestHeader("token") String token) {
        Boolean flage = contractInfoService.approveContractInfo(contractInfoVO,token,false);
        return ResponseAPI.successData(flage);
    }

    /**
     * 合同信息通过审批
     * @param projectID 项目ID
     * @return
     */
    @PostMapping("queryContractInfoByProjectId/{projectID}")
    public ResponseAPI queryContractInfoByProjectId(@PathVariable(value = "projectID") Integer projectID) {
        List<ContractInfoVO> contracts = contractInfoService.queryContractInfoByProjectId(projectID,null);
        return ResponseAPI.successData(contracts);
    }


    @PostMapping("queryContractInfoByProjectId/{projectID}/{callWms}")
    public ResponseAPI queryContractInfoByProjectId(@PathVariable(value = "projectID") Integer projectID,@PathVariable(value = "callWms") String callWms) {
        List<ContractInfoVO> contracts = contractInfoService.queryContractInfoByProjectId(projectID,callWms);
        return ResponseAPI.successData(contracts);
    }

    /**
     * 通过项目ID查询目前项目下 审批中或审批通过的合同总金额
     * @param projectId 项目ID
     * @return
     */
    @PostMapping("queryConMoneyByProjectId/{projectID}")
    public ResponseAPI queryConMoneyByProjectId(@PathVariable(value = "projectID") Integer projectId){
        return ResponseAPI.successData(contractInfoService.queryConMoneyByProjectId(projectId));
    }

    /**
     * 分页查新合同信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryContractsByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryContractsByPage( @PathVariable(value = "pageNum") int pageNum,
                                            @PathVariable(value = "pageSize") int pageSize,
                                            @RequestBody ContractInfoVO entity,
                                            @RequestHeader("token") String token) {
        IPage<ContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ContractInfoVO> result = contractInfoService.queryContractsByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    @PostMapping("/exportContractInfo")
    public void contractInfoExport(HttpServletResponse response,
                                   @RequestBody ContractInfoVO entit,
                                   @RequestHeader("token") String token) throws Exception{
        contractInfoService.exportContractInfo(response,entit,token);
    }

    /**
     * 分页查新回款计划
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryBackMoneyPlansByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryBackMoneyPlansByPage( @PathVariable(value = "pageNum") int pageNum,
                                             @PathVariable(value = "pageSize") int pageSize,
                                             @RequestBody BackMoneyPlanVo entity,
                                             @RequestHeader("token") String token) {
        IPage<BackMoneyPlanVo> page = new Page<>(pageNum, pageSize, true);
        PageUtil<BackMoneyPlanVo> result = contractInfoService.queryBackMoneyPlansByPage(page ,entity ,token);
        return ResponseAPI.successData(result);
    }

    @PostMapping("/exportBackMoneyPlans")
    public void exportBackMoneyPlans(HttpServletResponse response,
                                   @RequestBody BackMoneyPlanVo entit,
                                   @RequestHeader("token") String token) throws Exception{
        contractInfoService.exportBackMoneyPlans(response,entit,token);
    }

    /**
     * 更据采合同ID获取合同相关回款 开票记录数据
     * @param conId
     * @return
     */
    @PostMapping("/queryPurBackInfoByConId/{conId}")
    public ResponseAPI queryPurBackInfoByConId(@PathVariable(value = "conId") int conId){
        return ResponseAPI.successData(contractInfoService.queryPurBackInfoByConId(conId));
    }

    /**
     * 分页查询合同待办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalContractByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalContractInfoByPage( @PathVariable(value = "pageNum") int pageNum,
                                                       @PathVariable(value = "pageSize") int pageSize,
                                                       @RequestBody ContractInfoVO entity) {
        IPage<ContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ContractInfoVO> result = contractInfoService.queryApprovalContractByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询合同已办信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalContractInfoDoneByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalContractInfoDoneByPage( @PathVariable(value = "pageNum") int pageNum,
                                                           @PathVariable(value = "pageSize") int pageSize,
                                                           @RequestBody ContractInfoVO entity) {
        IPage<ContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ContractInfoVO> result = contractInfoService.queryApprovalContractDoneByPage(page ,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 分页查询合同申请记录信息
     * @param pageNum 页数
     * @param pageSize 每页显示数量
     * @param entity 实体条件
     * @return
     */
    @PostMapping("/queryApprovalContractInfoRecordByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalContractInfoRecordByPage( @PathVariable(value = "pageNum") int pageNum,
                                                             @PathVariable(value = "pageSize") int pageSize,
                                                             @RequestBody ContractInfoVO entity,
                                                             @RequestHeader("token") String token) {
        IPage<ContractInfoVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ContractInfoVO> result = contractInfoService.queryApprovalContractRecordByPage(page ,entity,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 删除合同单
     * @param contractId
     * @return
     */
    @PostMapping("/deleteContractInfoById/{contractId}")
    public ResponseAPI deleteContractInfoById(@PathVariable(value = "contractId") int contractId){
        return ResponseAPI.successData(contractInfoService.deleteContractInfoById(contractId));
    }
}
